package com.ruoyi.riskAssessment.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.riskAssessment.domain.RiskIdentifyEvaluation;
import com.ruoyi.riskAssessment.dto.RkCardDto;
import com.ruoyi.riskAssessment.mapper.RiskIdentifyEvaluationMapper;
import com.ruoyi.riskAssessment.vo.RiskIdentifyEvaluationVo;
import com.ruoyi.riskAssessment.vo.RkCardVo;
import com.ruoyi.system.domain.SysDictItem;
import com.ruoyi.system.mapper.SysDictItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.riskAssessment.mapper.RkCardMapper;
import com.ruoyi.riskAssessment.domain.RkCard;
import com.ruoyi.riskAssessment.service.IRkCardService;

/**
 * 风险-三卡管理Service业务层处理
 *
 * @author lth
 * @date 2023-12-15
 */
@Service
public class RkCardServiceImpl implements IRkCardService
{
    @Autowired
    private RkCardMapper rkCardMapper;

    @Autowired
    private RiskIdentifyEvaluationMapper riskIdentifyEvaluationMapper;

    @Autowired
    private SysDictItemMapper sysDictItemMapper;


    /**
     * 查询风险-三卡管理
     *
     * @param id 风险-三卡管理主键
     * @return 风险-三卡管理
     */
    @Override
    public RkCard selectRkCardById(Long id)
    {
        return rkCardMapper.selectRkCardById(id);
    }

    /**
     * 查询风险-三卡管理列表
     *
     * @param rkCard 风险-三卡管理
     * @return 风险-三卡管理
     */
    @Override
    public List<RkCard> selectRkCardList(RkCard rkCard)
    {
        return rkCardMapper.selectRkCardList(rkCard);
    }

    /**
     * 新增风险-三卡管理
     *
     * @param rkCard 风险-三卡管理
     * @return 结果
     */
    @Override
    public int insertRkCard(RkCard rkCard)
    {
        return rkCardMapper.insertRkCard(rkCard);
    }

    /**
     * 修改风险-三卡管理
     *
     * @param rkCard 风险-三卡管理
     * @return 结果
     */
    @Override
    public int updateRkCard(RkCard rkCard)
    {
        return rkCardMapper.updateRkCard(rkCard);
    }

    /**
     * 批量删除风险-三卡管理
     *
     * @param ids 需要删除的风险-三卡管理主键
     * @return 结果
     */
    @Override
    public int deleteRkCardByIds(Long[] ids)
    {
        return rkCardMapper.deleteRkCardByIds(ids);
    }

    /**
     * 删除风险-三卡管理信息
     *
     * @param id 风险-三卡管理主键
     * @return 结果
     */
    @Override
    public int deleteRkCardById(Long id)
    {
        return rkCardMapper.deleteRkCardById(id);
    }

    /**     三卡管理--制作卡牌--选择风险列表
     * @Author lth
     * @Description //TODO      vo传过来的cardId=1,与数据库中的cardId(1694278546560651265)不匹配
     * @Date 9:36 2023/12/20
     * @Param
     * @return
     **/
    @Override
    public List<RkCardDto> listByVo(RkCardVo vo) {
        String reportStatus = vo.getReportStatus();
        String assessmentLevel = vo.getAssessmentLevel();
        String cardId = vo.getCardId();

        List<RiskIdentifyEvaluation> riskIdentifyEvaluationList = riskIdentifyEvaluationMapper.listByVo(reportStatus,assessmentLevel,cardId);
        List<RkCardDto> rkCardDtoList = riskIdentifyEvaluationList.stream().map(riskIdentifyEvaluation -> {
            RkCardDto rkCardDto = new RkCardDto();
            rkCardDto.setRiskCode(riskIdentifyEvaluation.getRiskCode());
            rkCardDto.setLocationName(riskIdentifyEvaluation.getLocationName());
            rkCardDto.setRiskSourceDesc(riskIdentifyEvaluation.getRiskSourceDesc());

            //风险等级
            rkCardDto.setRiskLevel(null);
            //风险源类型
            String riskValue = riskIdentifyEvaluation.getRiskSourceTypeId();
            SysDictItem riskType = sysDictItemMapper.getRiskType(riskValue);
            rkCardDto.setRiskSourceTypeId_dictText(riskType.getItemText());
            //风险状态
            Long statusValue = riskIdentifyEvaluation.getStatus();
            SysDictItem status = sysDictItemMapper.getStatus(Math.toIntExact(statusValue));
            rkCardDto.setRiskStatusId_dicText(status.getItemText());
            //可能导致事故
            rkCardDto.setCauseAccident(riskIdentifyEvaluation.getCauseAccident());

            rkCardDto.setScheduleTaskId_dicText(null);
            return rkCardDto;
        }).collect(Collectors.toList());

        return rkCardDtoList;
    }

}
